package testtimer;

import java.io.Serializable;
import java.util.Collection;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Session Bean implementation class MockTimerBean
 */
@Stateless
public class MockTimerBean implements MockTimerBeanRemote {


	@Resource
	TimerService mockTimerService;
	
    @Resource (mappedName = "jms/candidateConn")
    private ConnectionFactory connectionFactory;
    
    @Resource (mappedName = "jms/candidateQueue")
    private Queue queue;
    
//    @Resource (mappedName = "jms/candidateTopic")
//    private Topic topic;
    
	private long timePassed = 0;
	
    public MockTimerBean() {
    }
    
    @Override
    public void startTimer(Serializable info) {
		System.out.println("MockTimer started");
		// from second to second
		mockTimerService.createTimer(5 * 1000, 5 * 1000, info);
		// from minute to minute
//		mockTimerService.createTimer(60 * 1000, 60 * 1000, info);
    }
    
    public void stopTimer(Serializable info) {
		Timer timer = null;
		Collection<Timer> timers = mockTimerService.getTimers();
		for (Object o : timers) {
			timer = ((Timer) o);
			timer.cancel();
 		}
		System.out.println("MockTimerBean stopped.");
    }
    
    public long getTimePassed() {
		return timePassed;
	}

	public void setTimePassed(long timePassed) {
		this.timePassed = timePassed;
	}

	@Timeout
    public void alarmOneSecond(Timer t) {
    	this.timePassed += 5 * 1000;
    	long time = this.timePassed / 1000;  
    	String seconds = Integer.toString((int)(time % 60));  
    	String minutes = Integer.toString((int)((time % 3600) / 60));  
    	String hours = Integer.toString((int)(time / 3600));  
    	for (int i = 0; i < 2; i++) {  
    	if (seconds.length() < 2) {  
    		seconds = "0" + seconds;  
    	}  
    	if (minutes.length() < 2) {  
    		minutes = "0" + minutes;  
    	}  
    	if (hours.length() < 2) {  
    		hours = "0" + hours;  
    	}  
    	}  
    	System.out.println("Time passed: " + this.timePassed);
    	String toBePassed = hours + ":" + minutes + ":" + seconds;
    	System.out.println("Message to be sent: " + toBePassed);
    	
        Connection conn = null;
        MessageProducer messageProducer = null;
        TextMessage textMessage = null;
        Session session = null;
		try {
	        conn = connectionFactory.createConnection();
	        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
	    	messageProducer = session.createProducer(queue);
//	        messageProducer = session.createProducer(topic);
    		textMessage = session.createTextMessage();
    		textMessage.setText(toBePassed);
    		messageProducer.send(textMessage);
    		messageProducer.close();
    		session.close();
    		conn.close();
		}
		catch (JMSException e) {
			e.printStackTrace();
		}
    
    }
}
